package accounts;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import powerpass.PowerPassSQL;

/**
 *
 * @author AaronFrost
 */
public abstract class BaseAccount {

    private int typeID, //0 = standard, 1 = financial
            accountID,
            clientID;
    private String accountName,
            accountDescription,
            username,
            password;
    private boolean active,
            accountIsNew = false,
            passwordHasChanged = false,
            activeHasChanged = false,
            haveAnyFieldsBeenUpdate = false;
    private Date created,
            lastMod;
    private ArrayList<String> passwordHistory;

    public int getTypeID() {
        return typeID;
    }

    public void setTypeID(int inTypeID) {
        if (getTypeID() != inTypeID) {
            typeID = inTypeID;
            setHaveAnyFieldsBeenChanged(true);
        }
    }

    public int getAccountID() {
        return accountID;
    }

    public void setAccountID(int inAccountID) {
        accountID = inAccountID;
    }

    public int getClientID() {
        return clientID;
    }

    public void setClientID(int inClientID) {
        clientID = inClientID;
    }

    public boolean getActive() {
        return active;
    }

    public void setActive(boolean incomingActive) {
        activeHasChanged = (incomingActive == active ? activeHasChanged : true);
        active = incomingActive;
    }

    public boolean getAccountIsNew() {
        return accountIsNew;
    }

    public void setAccountIsNew(boolean inAccountIsNew) {
        accountIsNew = inAccountIsNew;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date inCreated) {
        created = inCreated;
    }

    public Date getLastMod() {
        return lastMod;
    }

    public void setLastMod(Date inLastMod) {
        lastMod = inLastMod;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String inAccountName) {
        haveAnyFieldsBeenUpdate = (inAccountName.equalsIgnoreCase(accountName) ? haveAnyFieldsBeenUpdate : true);
        accountName = inAccountName;
    }

    public String getAccountDescription() {
        return accountDescription;
    }

    public void setAccountDescription(String inAccountDescription) {
        haveAnyFieldsBeenUpdate = (inAccountDescription.equalsIgnoreCase(accountDescription) ? haveAnyFieldsBeenUpdate : true);
        accountDescription = inAccountDescription;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String inUsername) {
        haveAnyFieldsBeenUpdate = (inUsername.equalsIgnoreCase(username) ? haveAnyFieldsBeenUpdate : true);
        username = inUsername;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String inPassword) {
        passwordHasChanged = (inPassword.equalsIgnoreCase(password) ? passwordHasChanged : true);
        password = inPassword;
    }

    public int getPasswordHistoryLength() {
        return passwordHistory.size();
    }


    public ArrayList getPasswordHistory() {
        return passwordHistory;
    }

    public boolean getPasswordHasChanged() {
        return passwordHasChanged;
    }

    public void setPasswordHasChanged(boolean inHasChanged) {
        passwordHasChanged = inHasChanged;
    }

    public void setHaveAnyFieldsBeenChanged(boolean inChanged) {
        haveAnyFieldsBeenUpdate = inChanged;
    }

    public void setActiveHasChanged(boolean inActiveHasChanged) {
        activeHasChanged = inActiveHasChanged;
    }

    public boolean getActiveHasChanged() {
        return activeHasChanged;
    }

    public abstract void Save();

    public abstract void Persist();

    public void InsertNewPasswordHistoryRecord() {
        //throw new UnsupportedOperationException("Not yet implemented");
        //Update passwordhistory.db
        //set closedDT = now
        //Where accountID = accountID
        //limit 1


        //Insert new record into passwordhistory.db, with the following info
        /**
         * passwordhistoryid = (autogen)
         * accountID = accountID
         * password = password
         * startDT = now
         * closedDT = 0000-00-00 00:00:00
         */
        setLastModDTToNow();
    }

    public void setLastModDTToNow() {
        //Update Accounts.db
        //Set lastModDT = now()
        //Where accountID = accountID
    }
    public void populatePasswordHistory(int inAccountid) throws SQLException
    {
        passwordHistory = new ArrayList<String>();
        PowerPassSQL pp = new PowerPassSQL();
        String SQL = "Select password "
                + "from slccpowerpass.accountpasshistory "
                + "where accountid = " + inAccountid + " "
                + "order by passhistoryid desc";
        ResultSet results = pp.selectInfo(SQL);
        while (results.next()) {
            passwordHistory.add(results.getString(1));
        }
        this.password = passwordHistory.get(0);
    }

}
